﻿using Dapper;
using IntelligentGateway.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentGateway.Data
{
    public class DeviceInfoDA
    {
        public MasterDeviceInfo GetMasterDeviceInfo()
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    MasterDeviceInfo devInfo = conn.QueryFirst<MasterDeviceInfo>("SELECT * FROM masterdeviceinfo");
                    return devInfo;
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }

        public List<MasterDeviceInfo> GetAllMasterDeviceInfo()
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    List<MasterDeviceInfo> devInfo = conn.Query<MasterDeviceInfo>("SELECT * FROM masterdeviceinfo").AsList();
                    return devInfo;
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
        public List<SubDeviceInfo> GetSubDeviceInfo(int PID)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    List<SubDeviceInfo> LstSubDeviceInfo = conn.Query<SubDeviceInfo>("SELECT * FROM subdeviceinfo where PID=@PID", new { PID = PID }).AsList();

                    return LstSubDeviceInfo;
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
        public string GetDeviceID(string IPAddress)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    ushort DeviceID = conn.ExecuteScalar<ushort>("SELECT NO FROM masterdeviceinfo where DeviceIP=@IPAddress");
                    if (DeviceID <= 0)
                    {
                        DeviceID = conn.QueryFirst("SELECT NO FROM subdeviceinfo where DeviceIP=@IPAddress");
                    }
                    if (DeviceID <= 0)
                    {
                        return "";
                    }
                    else
                    {
                        return DeviceID.ToString();
                    }
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
        public string GetDeviceIDFromPanel(string PanelIP)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    ushort DeviceID = conn.ExecuteScalar<ushort>("SELECT NO FROM masterdeviceinfo where PanelIP=@PanelIP");
                    if (DeviceID <= 0)
                    {
                        DeviceID = conn.QueryFirst("SELECT NO FROM subdeviceinfo where PanelIP=@PanelIP");
                    }
                    if (DeviceID <= 0)
                    {
                        return "";
                    }
                    else
                    {
                        return DeviceID.ToString();
                    }
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
        public string GetPanelIP(string DevNO)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    string PanelIP = conn.ExecuteScalar<string>("SELECT Indent FROM paneldeviceinfo d inner join MasterDeviceInfo m on d.DID=m.ID where m.NO=@NO", new { NO = DevNO });
                    if (string.IsNullOrEmpty(PanelIP))
                    {
                        PanelIP = conn.ExecuteScalar<string>("SELECT Indent FROM paneldeviceinfo d inner join subdeviceinfo s on d.DID=s.ID where s.NO=@NO", new { NO = DevNO });
                    }
                    if (string.IsNullOrEmpty(PanelIP))
                    {
                        return "";
                    }
                    else
                    {
                        return PanelIP;
                    }
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
    }
}
